package tests;




import gameComponents.Board;
import gameComponents.ChessGame;
import gameComponents.Player;
import gamePieces.Pawn;
import gamePieces.Piece;

import java.util.ArrayList;


import junit.framework.TestCase;


public class ChessGameMethodsTest extends TestCase {
	
	// Sets up game scenario with the user
	ChessGame game = new ChessGame();
	Player player1 = game.getPlayer1();
	Player player2 = game.getPlayer2();
	Player cplayer = game.getCurrentPlayer();
	Board board = game.getBoard();
	
	public ChessGameMethodsTest(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		super.setUp();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
	/**
	 * Tests three methods, checkSamePiece and pieceBetween in the ChessGame class.
	 * Tests the remove method by getting a piece, removing it from both the board and the player's
	 * HashMap. Also tests removing a piece that isn't there.
	 * @author Vivian Andreeva and Jordan Beland
	 */
	public void testMethods(){
		game.addPlayer1Pieces();
		game.addPlayer2Pieces();
		placeOnBoard();

		// The following method checks if the checkSamePiece in the ChessGame Class passes.
		
		// Pawn at position B7 (should return true because it is player 1's piece)
		assertEquals(cplayer.getId(), 1);
		assertEquals(game.checkSamePiece(1, 6), true);
		//Pawn at position B2 (should return false because it is player 2's piece)
		assertEquals(game.checkSamePiece(1,1), false);
		
		// The following method checks if the pieceBetween in the ChessGame Class passes.
		
		// Is there a piece between the Queen(E8) and (E6)? Yes! Pawn!
		assertEquals(game.pieceBetween(4,7,4,5), true);
		// Is there a piece between the Pawn(E7) and (E6)? NO!
		assertEquals(game.pieceBetween(4, 6, 4, 5), false);
		
		// The following method checks if the switchPlayer method in the ChessGame Class passes.
		
		// Current player is player 1
		assertEquals(cplayer.getId()==1, true);
		game.switchPlayer();
		// Now current player should be player 2
		assertEquals(game.getCurrentPlayer().getId()==1, false);
		assertEquals(game.getCurrentPlayer().getId()==2, true);	
		
		// Testing removePiece()
		
		// Piece at B1 is a pawn
		Piece p = game.getPiece(1, 1);
		assertTrue((p instanceof Pawn));
		Pawn pawn = (Pawn)p;
		// Pawn at (B1) is player2's
		assertTrue(pawn.getOwnership() == player2);
		// Pawn is in player's hashmap
		ArrayList<Piece> pawns = player2.returnMap().get("P");
		assertTrue(pawns.contains(pawn));
		// remove pawn from game
		assertTrue(game.removePiece(1,1));
		// pawn is no longer on the board
		p = game.getPiece(1,1);
		assertEquals(p,null);
		// pawn is no longer in player2's hashmap
		assertFalse(pawns.contains(pawn));
		// try to remove the piece from the game again
		assertFalse(game.removePiece(1,1));	
	}
	
	/**
	 * Helper method to place all the pieces on the board after they have been moved.
	 * @author Vivian Andreeva
	 */
	public void placeOnBoard() {
		game.placeOnBoard(player1);
		game.placeOnBoard(player2);
		board.print();
	}
}
